package com.mapbox.maps.testapp.integration.surface

import android.view.KeyEvent
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import com.mapbox.maps.testapp.examples.SurfaceActivity
import com.mapbox.maps.testapp.integration.BaseIntegrationTest
import com.mapbox.maps.testapp.integration.launchActivity
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Regression test that validates reopening an Activity with a surface using ContextMode.SHARED
 */
@RunWith(AndroidJUnit4::class)
class SurfaceReopenTest : BaseIntegrationTest() {

  @get:Rule
  var activityRule: ActivityScenarioRule<SurfaceActivity> = ActivityScenarioRule(
    SurfaceActivity::class.java
  )

  @Test
  @LargeTest
  fun reopenSurfaceActivity() {
    activityRule.scenario.onActivity {
      device.launchActivity(it, SurfaceActivity::class.java)
      device.waitForIdle()
    }
    for (i in 0 until testRepeatCount) {
      device.waitForIdle()
      device.pressHome()
      device.waitForIdle()
      // double click restores last opened application
      device.pressKeyCode(KeyEvent.KEYCODE_APP_SWITCH)
      device.pressKeyCode(KeyEvent.KEYCODE_APP_SWITCH)
      // some time to set things up
      Thread.sleep(2_000L)
    }
  }
}